package com.ruoyi.ora.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.dict.AiModel;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ora.constant.BizConstants;
import com.ruoyi.ora.domain.ChatRequest;
import com.ruoyi.ora.domain.ChatResponse;
import com.ruoyi.ora.domain.VoiceTranModel;
import com.ruoyi.ora.factory.AiFactory;
import com.ruoyi.ora.factory.VoiceTranslatFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ora.mapper.DeviceAichatMapper;
import com.ruoyi.ora.domain.DeviceAichat;
import com.ruoyi.ora.service.IDeviceAichatService;

/**
 * AI聊天Service业务层处理
 * 
 * @author liuwp
 * @date 2025-08-22
 */
@Service
public class DeviceAichatServiceImpl implements IDeviceAichatService 
{
    @Autowired
    private DeviceAichatMapper deviceAichatMapper;

    /**
     * 查询AI聊天
     * 
     * @param id AI聊天主键
     * @return AI聊天
     */
    @Override
    public DeviceAichat selectDeviceAichatById(Long id)
    {
        return deviceAichatMapper.selectDeviceAichatById(id);
    }

    /**
     * 查询AI聊天列表
     * 
     * @param deviceAichat AI聊天
     * @return AI聊天
     */
    @Override
    public List<DeviceAichat> selectDeviceAichatList(DeviceAichat deviceAichat)
    {
        return deviceAichatMapper.selectDeviceAichatList(deviceAichat);
    }

    /**
     * 根据业务主键查询一条记录：AI聊天
     *
     * @param deviceAichat AI聊天
     * @return AI聊天
     */
    @Override
    public DeviceAichat selectOneDeviceAichat(DeviceAichat deviceAichat)
    {
        return deviceAichatMapper.selectOneDeviceAichat(deviceAichat);
    }

    /**
     * 新增AI聊天
     * 
     * @param deviceAichat AI聊天
     * @return 结果
     */
    @Override
    public int insertDeviceAichat(DeviceAichat deviceAichat)
    {
        deviceAichat.setCreateTime(DateUtils.getNowDate());
        return deviceAichatMapper.insertDeviceAichat(deviceAichat);
    }

    /**
     * 修改AI聊天
     * 
     * @param deviceAichat AI聊天
     * @return 结果
     */
    @Override
    public int updateDeviceAichat(DeviceAichat deviceAichat)
    {
        return deviceAichatMapper.updateDeviceAichat(deviceAichat);
    }

    @Override
    public int updateDeviceAichatCondition(DeviceAichat deviceAichat, DeviceAichat deviceAichatCondition)
    {
        return deviceAichatMapper.updateDeviceAichatCondition(deviceAichat, deviceAichatCondition);
    }

    /**
     * 批量删除AI聊天
     * 
     * @param ids 需要删除的AI聊天主键
     * @return 结果
     */
    @Override
    public int deleteDeviceAichatByIds(Long[] ids)
    {
        return deviceAichatMapper.deleteDeviceAichatByIds(ids);
    }

    /**
     * 删除AI聊天信息
     * 
     * @param id AI聊天主键
     * @return 结果
     */
    @Override
    public int deleteDeviceAichatById(Long id)
    {
        return deviceAichatMapper.deleteDeviceAichatById(id);
    }

    /**
     * 条件删除AI聊天
     *
     * @param deviceAichat AI聊天
     * @return 结果
     */
    @Override
    public int deleteDeviceAichatByCondition(DeviceAichat deviceAichat)
    {
        return deviceAichatMapper.deleteDeviceAichatByCondition(deviceAichat);
    }

    @Override
    public DeviceAichat aichatAutoReply(List<DeviceAichat> deviceAichats) {

        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel(AiModel.HUNYUAN.getValue());

        List<ChatRequest.Message> messages = new ArrayList<>();

        ChatRequest.Message sysMessage = new ChatRequest.Message();
        sysMessage.setRole("system");
        sysMessage.setContent("你是一名智能AI聊天机器人，请根据用户的问题，进行智能回复。");
        messages.add(sysMessage);

        for (DeviceAichat deviceAichat : deviceAichats) {
            ChatRequest.Message message = new ChatRequest.Message();
            message.setRole("user");
            if (deviceAichat.getChatType() == 1){
                message.setContent(deviceAichat.getDeviceContent());
            }else {
                message.setContent(deviceAichat.getAichatContent());
            }
            messages.add(message);
        }
        chatRequest.setMessages(messages);

        // 根据设备内容，调用AI自动回复
        ChatResponse chatResponse = AiFactory.build(BizConstants.AI_MODEL.getValue()).chatCompletions(chatRequest);

        if (chatResponse != null && chatResponse.getError() == null) {
            // 组装回复内容
            DeviceAichat aichat = new DeviceAichat();
            aichat.setAichatContent(chatResponse.getChoices().get(0).getMessage().getContent());
            aichat.setDeviceSn(deviceAichats.get(0).getDeviceSn());
            aichat.setChatType(2);

            // 生成音频文件路径
            String voicePath =  "/hc_voice/" + DateUtils.datePath() + "/" + aichat.getDeviceSn() + "_" + System.currentTimeMillis() + ".mp3";

            VoiceTranModel voiceTranModel = VoiceTranslatFactory.build(BizConstants.TRAN_MODEL.getValue()).textTranVoice(aichat.getAichatContent(), voicePath);

            if (voiceTranModel != null) {
                aichat.setAichatVoice(voiceTranModel.getVoicePath());   // 音频文件路径 AI文字内容转音频
                aichat.setAichatDuration(voiceTranModel.getTotalDuration());  // AI 音频时长
            }
            return aichat;
        }
        return null;
    }


}
